<?php
namespace app\feelym\controller;

use app\common\controller\AdminBase;
use app\common\model\Article as ArticleModel;
use app\common\model\Articlecate as ArticlecateModel;
use app\common\model\Upload as UploadModel;
use think\Db;

class About extends AdminBase
{
    protected $category_model;
    protected $article_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->category_model = new ArticlecateModel();
        $this->article_model  = new ArticleModel();
        $category_level_list  = $this->category_model->catetree();
        $this->assign('category_level_list', $category_level_list);
    }

    public function index()
    {
        $page =  isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
        $perpage =  isset($_REQUEST['perpage']) ? $_REQUEST['perpage'] : 10;
        $keyword=isset($_REQUEST['keyword']) ? trim($_REQUEST['keyword']) : '';
        $tid=isset($_REQUEST['tid']) ? trim($_REQUEST['tid']) : 0;
        $open=isset($_REQUEST['open']) ? trim($_REQUEST['open']) : '';
        $settop=isset($_REQUEST['settop']) ? trim($_REQUEST['settop']) : '';
        $choice=isset($_REQUEST['choice']) ? trim($_REQUEST['choice']) : '';
        $startdate=isset($_REQUEST['startdate']) ? trim($_REQUEST['startdate']) : '';
        $enddate=isset($_REQUEST['enddate']) ? trim($_REQUEST['enddate']) : '';
        $map = [];

        if ($keyword) {      
            $map['title|f.keywords'] = ['like', "%{$keyword}%"];
        }
        if ($tid!='') {     
            $map['f.tid'] = $tid;
        }
        
        if ($open!='') {        
            $map['open'] = $open;
        }  
        if ($settop!='') {        
            $map['settop'] = $settop;
        }
        if ($choice!='') {        
            $map['choice'] = $choice;
        } 
        if ($keyword) {      
            $map['title|f.keywords'] = ['like', "%{$keyword}%"];
        }
        if ($startdate) {      
            $map['time'] = ['egt', strtotime($startdate)];
        }
        if ($enddate) {      
            $map['time'] = ['elt', strtotime($enddate.' 23:59:59')];
        }     
        $carousel = Db::name('about')->where('open', 1)->order('time desc')->select();
        $this->assign('carousel',$carousel);
        $this->assign(array('choice' => $choice,'settop' => $settop,'open' => $open,'tid' => $tid,'keyword' => $keyword,'startdate' => $startdate,'enddate' => $enddate,'perpage' => $perpage)); 

        return view();
    }
    public function add()
    {
        return view();
    }
    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
        
            $data['time']=time();
            $data['open'] = '1';
            // 验证通过后删除token，禁止带入数据库
            unset($data['token']);
            $res = Db::name('about')->where(['id'=>$data['id']])->insert($data);
            if($res){
                return json(array('code' => 200, 'msg' => '添加成功'));
            }
        } else {
                return json(array('code' => 0, 'msg' => '添加失败'));
        }
    }

    /**
     * 编辑分类
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {

        $tptcs = Db::name('about')->where('id', $id)->select();
        $this->assign(array('tptcs' => $tptcs));
        return $this->fetch('edit', ['tptca' => $tptcs[0]]);
        // return view();
    }

    /**
     * 更新分类
     * @throws \think\Exception
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
        
            $data['time']=time();
            $data['open'] = '1';
            // 验证通过后删除token，禁止带入数据库
            unset($data['token']);
            $res = Db::name('about')->where(['id'=>$data['id']])->update($data);
            if($res){
                return json(array('code' => 200, 'msg' => '更新成功'));
            }
        } else {
                return json(array('code' => 0, 'msg' => '更新失败'));
        }
       
    }

    /**
     * 删除分类
     * @param $id
     * @throws \think\Exception
     */
    public function delete($id)
    {

        $info  = $this->article_model->find($id);
        $score = getpoint($info['uid'], 'articleadd', $id);
        point_note(0 - $score, $info['uid'], 'articledelete', $id);

        if ($this->article_model->destroy($id)) {

            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    public function alldelete()
    {
        $params = input('post.');
        foreach ($params['ids'] as $k => $v) {
            $info  = $this->article_model->find($v);
            $score = getpoint($info['uid'], 'articleadd', $v);
            point_note(0 - $score, $info['uid'], 'articledelete', $v);

        }

        $ids    = implode(',', $params['ids']);
        $result = $this->article_model->destroy($ids);
        if ($result) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }

    public function setClick()
    {
        $params = input('post.');
        foreach ($params['ids'] as $k => $v) {
            $info  = $this->article_model->find($v);
            $score = getpoint($info['uid'], 'articleadd', $v);
            point_note(0 - $score, $info['uid'], 'articledelete', $v);
        }

        $ids    = implode(',', $params['ids']);
        $result = $this->article_model->destroy($ids);
        if ($result) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
}
